/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package station;

/**
 *
 * @author safe
 */
public final class DataEmpfangeneNachrichten {

    private static String ourStationName = "";
    public static String lastMessageStationName = "";
    private static String lastMessageClass = "";
    private static int lastMessageSlot = -1;
    private static long lastMessageSendTime = 0;
    private static long lastMessageReceiveTime = 0;
    private static long lastCollisionTimestamp = 0;
//    private static long lastSending = 0;
    public static final int diffForDetectCollision = 15;
    private static boolean sendingCollision = false;

    private DataEmpfangeneNachrichten() {
    }

    public static synchronized String getLastMessageStationName() {
        return lastMessageStationName;
    }

//    public static long getLastSending() {
//        return lastSending;
//    }
//
//    public static void setLastSending(long lastSending) {
//        DataEmpfangeneNachrichten.lastSending = lastSending;
//    }
    public static String getOurStationName() {
        return ourStationName;
    }

    public static void setOurStationName(String ourStationName) {
        DataEmpfangeneNachrichten.ourStationName = ourStationName;
    }

    public static void setSendingCollision(boolean bool) {
        sendingCollision = bool;
    }

    public static boolean getSendingCollision() {
        return sendingCollision;
    }

    public static String getLastMessageClass() {
        return lastMessageClass;
    }

    public static int getLastMessageSlot() {
        return lastMessageSlot;
    }

    synchronized static boolean collisionWithUs(String stationName) {
        return ourStationName.equals(stationName);
    }

    synchronized static boolean collisionDetect(long actualMessageReceiveTime) {
        // wir brauchen auch diesen vergleich, weil lastMessageReceiveTime eventuell 0 ist nach collisionCleanUp

        if (lastCollisionTimestamp != 0 && getSlotNumber(lastCollisionTimestamp) == getSlotNumber(actualMessageReceiveTime)) {
            return true;

        } else if (lastMessageReceiveTime != 0 && getSlotNumber(lastMessageReceiveTime) == getSlotNumber(actualMessageReceiveTime)) {
            return true;

        } else {
            return false;
        }
    }

    static int getSlotNumber(long timestamp) {
        int slotnummer = (int) (timestamp % 1000) / 40;
        return slotnummer;
    }

    synchronized static void collisionCleanUp(long actualMessageReceiveTime) {
        lastCollisionTimestamp = actualMessageReceiveTime;
        //lastMessageStationName = "";
        lastMessageClass = "";
        lastMessageSlot = -1;
        lastMessageSendTime = 0;
        lastMessageReceiveTime = 0;
    }
    
    synchronized static void prepareForFrameEnd(){
        lastCollisionTimestamp = 0;
        lastMessageStationName = "";
        lastMessageClass = "";
        lastMessageSlot = -1;
        lastMessageSendTime = 0;
        lastMessageReceiveTime = 0;
    }

    static synchronized void setLastMessageAsValid(String stationName,
            String stationClass,
            int slotNumber,
            long sendTimeStamp,
            long receiveTimestamp) {
        lastMessageStationName = stationName;
        lastMessageClass = stationClass;
        lastMessageSlot = slotNumber;
        lastMessageSendTime = sendTimeStamp;
        lastMessageReceiveTime = receiveTimestamp;
    }
}
